<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <title>部门树选择</title>
    <link href="/static/css/bootstrap.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="/static/include/css/style.css">
    <link href="/static/ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css" rel="stylesheet"/>
</head>
<style>
    body{height:auto;font-family: "Microsoft YaHei";}
    button{font-family: "SimSun","Helvetica Neue",Helvetica,Arial;}
    .btn-box{
        margin-top: 10px;
        margin-left: 20px;
        
    }
</style>
<body class="hold-transition box box-main">
<input id="treeId"   name="treeId"    type="hidden" th:value="${dept.deptId}"/>
<input id="treeName" name="treeName"  type="hidden" th:value="${dept.name}"/>
<div class="wrapper"><div class="treeShowHideButton" onclick="$.tree.toggleSearch();">
    <label id="btnShow" title="显示搜索" style="display:none;">︾</label>
    <label id="btnHide" title="隐藏搜索">︽</label>
</div>

    <div class="treeSearchInput" id="search">
        <label for="keyword">关键字：</label><input type="text" class="empty" id="keyword" maxlength="50">
        <button class="btn" id="btn" onclick="$.tree.searchNode()"> 搜索 </button>
    </div>
    <div class="btn-box">
        <a class="btn btn-primary btn-sm" id="checkAllTrue" href="#" >全选</a>
        <a class="btn btn-danger btn-sm" id="checkAllFalse" href="#">取消全选</a>
    </div>
    <div class="treeExpandCollapse">
        <a href="#" onclick="$.tree.expand()">展开</a> /
        <a href="#" onclick="$.tree.collapse()">折叠</a>
    </div>
    <div id="tree" class="ztree treeselect"></div>
</div>
<script src="/static/include/js/footer.js"></script>
<script src="/static/ajax/libs/jquery-ztree/3.5/js/jquery.ztree.all-3.5.js"></script>
<script>
    var deptIds;
    function initData(initData) {
        deptIds = initData
        initTree()
    }
   function initTree() {
       var url = api + "/admin/dept/tree";
       var options = {
           url: url,
           expandLevel: 1,
           check: {
               enable: true,             // 置 zTree 的节点上是否显示 checkbox / radio
               nocheckInherit: false,      // 设置子节点是否自动继承
               chkStyle:  "checkbox",
               // chkboxType: { "Y" : "s", "N" : "s" },
           },
           initData: deptIds,
           onClick : zOnClick
       };
       $.tree.init(options);
       // setChecked()
   }

    function setChecked() {

        deptIds.forEach(function(id) {
            var node =  $._tree.getNodeByParam("id",id)
            $._tree.checkNode(node)
        })
    }

    // function zOnClick(event, treeId, treeNode) {
    //     var treeId = treeNode.id;
    //     var treeName = treeNode.name;
    //     $("#treeId").val(treeId);
    //     $("#treeName").val(treeName);
    // }
	function zOnClick(event, treeId, treeNode) {
		var treeId = treeNode.id;
		var treeName = treeNode.name;
		if (treeNode.children){
			$._tree.checkNode(treeNode)
		}
		$("#treeId").val(treeId);
		$("#treeName").val(treeName);
	}

	$(function () {
	    $('#checkAllTrue').click(function(){
          $._tree.checkAllNodes(true)
      })
      $('#checkAllFalse').click(function(){
          $._tree.checkAllNodes(false)
      })
  })
</script>
</body>
</html>
